package com.visionblue.business.ship.page.dialog

import android.app.Activity
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import com.kennyc.view.MultiStateView
import com.visionblue.business.ship.R
import com.visionblue.business.ship.model.bean.response.ApplicationDetail
import com.visionblue.business.ship.model.enums.ApplicationRecordStatusEnum
import com.visionblue.business.ship.model.enums.ApplicationTypeEnum
import com.visionblue.business.ship.model.enums.WasteTypeEnum
import com.visionblue.business.ship.util.FormatUtils
import com.visionblue.business.ship.util.StringUtils
import com.visionblue.business.ship.util.ext.setVisible
import java.lang.ref.WeakReference

/**
 * @author: xlh
 * @date: 2020-05-21 6:04 PM
 * @desc:
 */
class TaskSlideDialog(context: Activity) {

    private val mContext = WeakReference(context)

    private var mDialog: BlurrySlideDialog? = null
//            = BlurrySlideDialog(
//        context,
//        R.layout.layout_task_order_assigned_to_me
//    )

    private fun initDialog() {
        if (mDialog == null) {
            val context = mContext.get() ?: return
            mDialog = BlurrySlideDialog(context, R.layout.layout_task_order_assigned_to_me)
        }
    }

    fun clear(){
        mDialog = null
    }


    fun showContent(
        detail: ApplicationDetail,
        hasNext: Boolean,
        hasPre: Boolean,
        onViewsClickListener: OnViewsClickListener? = null
    ) {

        initDialog()

        val dialog = mDialog ?: return

        dialog.findViewById<MultiStateView>(R.id.multiStateView).viewState =
            MultiStateView.ViewState.CONTENT

        resetState()

        val recordStatusEnum = ApplicationRecordStatusEnum.getRecordStatusEnum(detail.status)

        // 商船名称
        dialog.showText(R.id.tvShipName, "接收对象：" + detail.shipInfo?.shipName)

        // 状态
        dialog.showText(R.id.tvStatus, recordStatusEnum?.statusText)

        // 根据状态来决定操作按钮的显示
        if (recordStatusEnum == ApplicationRecordStatusEnum.PENDING) {
            // 待接单状态  此时按钮为"确认接收"
            dialog.findViewById<Button>(R.id.btnYes).run {
                setText(R.string.sure_to_collect)
                visibility = View.VISIBLE
                setOnClickListener {
                    onViewsClickListener?.onSureReceiveClick(detail.id,detail)
                }
            }

            // 待接单状态  此时按钮为"委婉拒绝"
            dialog.findViewById<Button>(R.id.btnNo).run {
                setText(R.string.decline)
                visibility = View.VISIBLE
                setOnClickListener {
                    onViewsClickListener?.onDeclineClick(detail.id)
                }
            }
        } else if (recordStatusEnum == ApplicationRecordStatusEnum.PROCESSING) {
            // 进行中   开始接收
            dialog.findViewById<Button>(R.id.btnYes).run {
                setText(R.string.start_receive)
                visibility = View.VISIBLE
                setOnClickListener {
                    onViewsClickListener?.onStartCollectClick(detail)
                    dialog.dismiss()
                }
            }
        }

        dialog.setColor(
            R.id.tvStatus,
            if (recordStatusEnum?.isGreen() == true) R.color.green0D else R.color.yellowF5
        )

        // 期望接驳时间段
        val workTime = detail.workTime
        if (workTime == null) {
            dialog.showText(
                R.id.tvDate, StringUtils.getString(
                    R.string.expected_in_date_format,
                    "-"
                )
            )
        } else {
            val startTime = FormatUtils.formatTimeYMDHHmm(workTime)
            val endTime = FormatUtils.formatTimeHHmm(workTime + 60 * 60 * 1000)
            dialog.showText(
                R.id.tvDate, StringUtils.getString(
                    R.string.expected_in_date_format,
                    "$startTime-$endTime"
                )
            )
        }

        // 污染物信息
        val wasteTypeInfoText =
            detail.wasteInfos?.map { "${WasteTypeEnum.getWasteType(it.key)?.desc}  ${it.value.toPlainString()} 吨" }
                ?.toList()
                ?.joinToString(separator = "\n")

        dialog.showText(R.id.tvWasteInfo, wasteTypeInfoText)

        // poi
        dialog.showText(R.id.tvPoiName, detail.poiName ?: "-")

        // address
        dialog.showText(R.id.tvAddress, detail.address ?: "-")

        // gps
        dialog.showText(
            R.id.tvGps,
            StringUtils.getString(
                R.string.latitude_longitude_format,
                FormatUtils.formatGps(detail.longitude) ?: "-",
                FormatUtils.formatGps(detail.latitude) ?: "-"
            )
        )

        // 自己的船名
        dialog.showText(R.id.tvMineInTransportOrder, "转出对象：" + detail.receiveShipName)

        // 下一个  上一个按钮显示
        dialog.findViewById<TextView>(R.id.tvNext).run {
            setVisible(hasNext)
            setOnClickListener {
                onViewsClickListener?.onNextClick()
            }
        }

        dialog.findViewById<TextView>(R.id.tvPre).run {
            setVisible(hasPre)
            setOnClickListener {
                onViewsClickListener?.onPreClick()
            }
        }

        dialog.findViewById<ImageView>(R.id.ivClose).setOnClickListener {

            onViewsClickListener?.onClose()
            dialog.dismiss()

        }

        dialog.setOnDismissListener {
            onViewsClickListener?.onDismiss()
        }

        dialog.setCanceledOnTouchOutside(false)

        if (ApplicationTypeEnum.getApplicationType(detail.recordType) == ApplicationTypeEnum.TRANSPORT) {
            dialog.findViewById<View>(R.id.tvMineInTransportOrder).setVisible(true)
            dialog.findViewById<View>(R.id.tvDate).setVisible(false)
            dialog.findViewById<View>(R.id.tvWasteInfo).setVisible(false)
            dialog.findViewById<View>(R.id.tvPoiName).setVisible(false)
            dialog.findViewById<View>(R.id.tvAddress).setVisible(false)
            dialog.findViewById<View>(R.id.tvGps).setVisible(false)
        } else {
            dialog.findViewById<View>(R.id.tvMineInTransportOrder).setVisible(false)
            dialog.findViewById<View>(R.id.tvDate).setVisible(true)
            dialog.findViewById<View>(R.id.tvWasteInfo).setVisible(true)
            dialog.findViewById<View>(R.id.tvPoiName).setVisible(true)
            dialog.findViewById<View>(R.id.tvAddress).setVisible(true)
            dialog.findViewById<View>(R.id.tvGps).setVisible(true)
        }

        dialog.show()
    }

    private fun resetState() {

        val dialog = mDialog ?: return

        dialog.findViewById<Button>(R.id.btnYes).visibility = View.GONE
        dialog.findViewById<Button>(R.id.btnNo).visibility = View.GONE

    }

    fun showEmpty() {

        initDialog()

        val dialog = mDialog ?: return

        dialog.findViewById<MultiStateView>(R.id.multiStateView).viewState =
            MultiStateView.ViewState.EMPTY
        dialog.findViewById<ImageView>(R.id.ivClose).setOnClickListener {
            dialog.dismiss()
        }
        dialog.show()
    }

    fun showLoading() {
        initDialog()

        val dialog = mDialog ?: return

        dialog.findViewById<MultiStateView>(R.id.multiStateView).viewState =
            MultiStateView.ViewState.LOADING

        dialog.findViewById<ImageView>(R.id.ivClose).setOnClickListener {
            dialog.dismiss()
        }
        dialog.show()
    }

    fun showError() {
        initDialog()
        val dialog = mDialog ?: return

        dialog.findViewById<MultiStateView>(R.id.multiStateView).viewState =
            MultiStateView.ViewState.ERROR

        dialog.findViewById<ImageView>(R.id.ivClose).setOnClickListener {
            dialog.dismiss()
        }

        dialog.show()
    }

    interface OnViewsClickListener {
        /**
         *  开始接收按钮被点击
         */
        fun onStartCollectClick(detail: ApplicationDetail)

        /**
         * 委婉拒绝点击
         */
        fun onDeclineClick(id: String?)

        /**
         * 确认接收被点击
         */
        fun onSureReceiveClick(id: String?, detail: ApplicationDetail)

        /**
         * 上一个
         */
        fun onPreClick()

        /**
         * 下一个
         */
        fun onNextClick()

        /**
         * 对话框消失
         */
        fun onDismiss()
        /**
         * 对话框主动关闭
         */
        fun onClose()

    }


}